package com.efounder.adapter;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import android.view.ViewGroup;

import com.efounder.fragment.MallOrderFragment;
import com.efounder.fragment.MallOrderToPayFragment;

/**
 * Created by will on 18-2-2.
 */

public class OrderPagerAdapter extends FragmentPagerAdapter {

    private String[] titles;
    FragmentManager mFm;
    public OrderPagerAdapter(FragmentManager fm, String[] titles) {
        super(fm);
        mFm=fm;
        this.titles = titles;
    }

    @Override
    public Fragment getItem(int position) {
        if (position == 0) {
            MallOrderFragment mallOrderFragment = new MallOrderFragment();
            Bundle bundle = new Bundle();
            bundle.putString("status", "all");
            mallOrderFragment.setArguments(bundle);
            return mallOrderFragment;
        }else if (position == 1) {
            MallOrderFragment mallOrderFragment = new MallOrderFragment();
            Bundle bundle = new Bundle();
            bundle.putString("status", OrderAdapter.MALL_NEED_PAY);
            mallOrderFragment.setArguments(bundle);
            return mallOrderFragment;
        }else if (position == 2) {
            MallOrderFragment mallOrderFragment = new MallOrderFragment();
            Bundle bundle = new Bundle();
            bundle.putString("status", OrderAdapter.MALL_NEED_SEND_GOOD);
            mallOrderFragment.setArguments(bundle);
            return mallOrderFragment;
        }else if(position == 3){
            MallOrderFragment mallOrderFragment = new MallOrderFragment();
            Bundle bundle = new Bundle();
            bundle.putString("status", OrderAdapter.MALL_NEED_RECEIVE);
            mallOrderFragment.setArguments(bundle);
            return mallOrderFragment;
        }else if (position == titles.length - 1){
            MallOrderFragment mallOrderFragment = new MallOrderFragment();
            Bundle bundle = new Bundle();
            bundle.putString("status", OrderAdapter.MALL_NEED_COMMENT);
            mallOrderFragment.setArguments(bundle);
            return mallOrderFragment;
        }
        return new MallOrderToPayFragment();
    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment f= (Fragment) super.instantiateItem(container, position);
        String fragmentTag = f.getTag();
        if (f != getItem(position)) {
            //如果是新建的fragment，f 就和getItem(position)是同一个fragment，否则进入下面
            FragmentTransaction ft =mFm.beginTransaction();
            //移除旧的fragment
            ft.remove(f);
            //换成新的fragment
            f =getItem(position);
            //添加新fragment时必须用前面获得的tag
            ft.add(container.getId(), f, fragmentTag);
            ft.attach(f);
            ft.commitAllowingStateLoss();
        }
        return f;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }
}
